package cn.com.nes.site.service.crm.log.impl;

import cn.com.nes.common.util.id.IdUtils;
import cn.com.nes.mybatis.agent.crm.entity.LogOperate;
import cn.com.nes.mybatis.agent.crm.mapper.LogOperateMapper;
import cn.com.nes.site.entity.em.crm.log.LogOperateTypeEnum;
import cn.com.nes.site.service.crm.clue.bo.ClueBO;
import cn.com.nes.site.service.crm.customer.bo.CustomerBO;
import cn.com.nes.site.service.crm.log.LogOperateService;
import cn.com.nes.site.service.crm.log.bo.LogOperateBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class LogOperateServiceImpl extends ServiceImpl<LogOperateMapper, LogOperate> implements LogOperateService {


    /**
     * 保存 操作日志
     * @param logOperateBO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveLogOperate(LogOperateBO logOperateBO) {
        this.saveOrUpdate(buildLogOperate(logOperateBO));
    }

    /**
     * 构建 操作日志对象
     * @param bo
     * @return
     */
    private LogOperate buildLogOperate(LogOperateBO bo) {
        LogOperate logOperate = new LogOperate();
        logOperate.setUuid(IdUtils.uuid());
        logOperate.setCustomerid(bo.getCustomerUuid());
        logOperate.setClueid(bo.getClueUuid());
        logOperate.setOpportunityid(bo.getOpportunityUuid());
        logOperate.setProjectid(bo.getProjectUuid());
        logOperate.setType(bo.getLogType().getKey());
        logOperate.setCreatetime(bo.getOperateTime());
        logOperate.setUserid(String.valueOf(bo.getOperateUserId()));
        logOperate.setUsername(bo.getOperateUserName());
        logOperate.setCompanyid(bo.getCompanyId());
        logOperate.setRemark(bo.getRemark());
        logOperate.setMemo(bo.getMemo());
        return logOperate;
    }

    /**
     * 构建 线索操作日志 业务对象
     * @param clueBO
     * @return
     */
    @Override
    public LogOperateBO buildLogOperate(ClueBO clueBO) {
        log.debug("构建线索日志操作模型");
        LogOperateBO logOperateBO = new LogOperateBO();
        logOperateBO.setClueUuid(clueBO.getClueSaveBO().getUuid());
        logOperateBO.setOperateUserId(clueBO.getOperateUserId());
        logOperateBO.setOperateUserName(clueBO.getOperateUserName());
        logOperateBO.setCompanyId(clueBO.getOperateUserCompanyId());
        logOperateBO.setLogType(LogOperateTypeEnum.CLUE);
        logOperateBO.setOperateTime(clueBO.getOperateDate());
        if (clueBO.isAdd()) {
            logOperateBO.setMemo(clueBO.getUserInfo()
                    .getCompanyName() + " " + clueBO.getOperateUserName() + "新增 " + clueBO.getClueSaveBO().getClueContact() + " 线索");
            logOperateBO.setRemark("新增线索");

        } else {
            logOperateBO.setMemo(clueBO.getUserInfo()
                    .getCompanyName() + " " + clueBO.getOperateUserName() + "修改了 " + clueBO.getClueSaveBO().getClueContact() + " 线索");
            logOperateBO.setRemark("修改线索");
        }
        return logOperateBO;
    }

    /**
     * 构建 客户操作日志 业务对象
     * @param customerBO
     * @return
     */
    @Override
    public LogOperateBO buildLogOperate(CustomerBO customerBO) {
        log.debug("构建 客户操作日志 模型");
        LogOperateBO logOperateBO = new LogOperateBO();
        logOperateBO.setCustomerUuid(customerBO.getUuid());
        logOperateBO.setOperateUserId(customerBO.getOperateUserId());
        logOperateBO.setOperateUserName(customerBO.getOperateUserName());
        logOperateBO.setCompanyId(customerBO.getOperateUserCompanyId());
        logOperateBO.setLogType(LogOperateTypeEnum.CUSTOMER);
        logOperateBO.setOperateTime(customerBO.getOperateDate());
        if (customerBO.addCustomer()) {
            logOperateBO.setMemo(customerBO.getUserInfo()
                    .getCompanyName() + " " + customerBO.getOperateUserName() + "新增 " + customerBO.getCustomerName() + " 客户");
            logOperateBO.setRemark("新增客户");

        } else {
            logOperateBO.setMemo(customerBO.getUserInfo()
                    .getCompanyName() + " " + customerBO.getOperateUserName() + "修改了 " + customerBO.getCustomerName() + " 客户");
            logOperateBO.setRemark("修改客户");
        }
        return logOperateBO;
    }

    /**
     * 构建 商机操作日志 业务对象
     * @param opportunityBO
     * @return
     */
    @Override
    public LogOperateBO buildLogOperate(OpportunityBO opportunityBO) {
        log.debug("构建 商机操作日志 模型");
        LogOperateBO logOperateBO = initializeLogOperateBO(opportunityBO);

        String companyName = opportunityBO.getUserInfo().getCompanyName();
        String operateUserName = opportunityBO.getOperateUserName();
        String opportunityName = opportunityBO.getOpportunityName();

        if (opportunityBO.addOpportunity()) {
            logOperateBO.setMemo(companyName + " " + operateUserName + "新增 " + opportunityName + " 商机");
            logOperateBO.setRemark("新增商机");
        } else {
            logOperateBO.setMemo(companyName + " " + operateUserName + "修改了 " + opportunityName + " 商机");
            logOperateBO.setRemark("修改商机");
        }

        return logOperateBO;
    }

    private LogOperateBO initializeLogOperateBO(OpportunityBO opportunityBO) {
        LogOperateBO logOperateBO = new LogOperateBO();
        logOperateBO.setOpportunityUuid(opportunityBO.getUuid());
        logOperateBO.setOperateUserId(opportunityBO.getOperateUserId());
        logOperateBO.setOperateUserName(opportunityBO.getOperateUserName());
        logOperateBO.setCompanyId(opportunityBO.getOperateUserCompanyId());
        logOperateBO.setLogType(LogOperateTypeEnum.OPPORTUNITY);
        logOperateBO.setOperateTime(opportunityBO.getOperateDate());
        return logOperateBO;
    }

}
